package net.zjitc.android2019.ch2activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.zjitc.android2019.R;

public class ch2Calc extends AppCompatActivity {
    EditText ed;
    TextView tv;
    Button btn_back, btn_clear, btn_7, btn_8, btn_9, btn_div;
    Button btn_4, btn_5, btn_6, btn_mul, btn_1, btn_2, btn_3, btn_sub;
    Button btn_0, btn_point, btn_equal, btn_add;
    /*2个操作数.操作符*/
    double n1,n2;
    String op;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch2_calc);
        init();

        View.OnClickListener click = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ch2_calc_btn_0:
                    case R.id.ch2_calc_btn_1:
                    case R.id.ch2_calc_btn_2:
                    case R.id.ch2_calc_btn_3:
                    case R.id.ch2_calc_btn_4:
                    case R.id.ch2_calc_btn_5:
                    case R.id.ch2_calc_btn_6:
                    case R.id.ch2_calc_btn_7:
                    case R.id.ch2_calc_btn_8:
                    case R.id.ch2_calc_btn_9:
                        number(v);
                        break;
                    case R.id.ch2_calc_btn_point:
                        point();
                        break;
                    case R.id.ch2_calc_btn_add:
                    case R.id.ch2_calc_btn_sub:
                    case R.id.ch2_calc_btn_mul:
                    case R.id.ch2_calc_btn_div:
                        Calculation(v);
                        break;
                    case R.id.ch2_calc_btn_equal:
                        equal();
                        break;
                    case R.id.ch2_calc_btn_back:
                        back();
                        break;
                    case R.id.ch2_calc_btn_clear:
                        clear();
                        break;


                }
            }
        };


        btn_0.setOnClickListener(click);
        btn_1.setOnClickListener(click);
        btn_2.setOnClickListener(click);
        btn_3.setOnClickListener(click);
        btn_4.setOnClickListener(click);
        btn_5.setOnClickListener(click);
        btn_6.setOnClickListener(click);
        btn_7.setOnClickListener(click);
        btn_8.setOnClickListener(click);
        btn_9.setOnClickListener(click);
        btn_back.setOnClickListener(click);
        btn_clear.setOnClickListener(click);
        btn_add.setOnClickListener(click);
        btn_sub.setOnClickListener(click);
        btn_mul.setOnClickListener(click);
        btn_div.setOnClickListener(click);
        btn_point.setOnClickListener(click);
        btn_equal.setOnClickListener(click);

    }

    private void clear() {
        ed.setText("");
        tv.setText("");
    }

    private void back() {
        String s = ed.getText().toString();
        ed.setText(s.substring(0,s.length()-1));
        ed.setSelection(s.length()-1);
    }

    private void equal() {
        double cut = 0;
        String s = ed.getText().toString();
        n2=Double.parseDouble(s);
        if (op.equals("+")){
            cut=n1+n2;
        }
        if (op.equals("-")){
            cut=n1-n2;
        }
        if (op.equals("/")){
            cut=n1/n2;
        }
        if (op.equals("*")){
            cut=n1*n2;
        }
        tv.setText(n1+op+n2+"="+cut);
        ed.setText("");
    }

    private void Calculation(View v) {
        String s1 = ed.getText().toString();
        n1=Double.parseDouble(s1);
        Button button = (Button) v;
        op=button.getText().toString();

        tv.setText(n1+op);
        ed.setText("");
    }

    private void point() {
        /*char[] ss=ed.getText().toString().toCharArray();
        if (ss[ss.length-1]!='.' & ss.length>=0) {
            String s = ed.getText().toString() + ".";
            ed.setText(s);
            ed.setSelection(s.length());
        }*/
        String s = ed.getText().toString();
        if (!s.contains(".")){
            s=s+".";
            ed.setText(s);
            ed.setSelection(s.length());
        }

    }

    /**
     * 点击0到9数字按钮时，输入框内显示值为：原来的值加上新点击的数字，注意将光标定位在最后。
     * */
    private void number(View v) {
        Button button = (Button) v;
        String s =ed.getText().toString()+button.getText().toString();
        ed.setText(s);
        ed.setSelection(s.length());
    }

    private void init() {
        ed = findViewById(R.id.ch2_calc_ed);
        tv = findViewById(R.id.ch2_calc_tv2);
        btn_0 = findViewById(R.id.ch2_calc_btn_0);
        btn_1 = findViewById(R.id.ch2_calc_btn_1);
        btn_2 = findViewById(R.id.ch2_calc_btn_2);
        btn_3 = findViewById(R.id.ch2_calc_btn_3);
        btn_4 = findViewById(R.id.ch2_calc_btn_4);
        btn_5 = findViewById(R.id.ch2_calc_btn_5);
        btn_6 = findViewById(R.id.ch2_calc_btn_6);
        btn_7 = findViewById(R.id.ch2_calc_btn_7);
        btn_8 = findViewById(R.id.ch2_calc_btn_8);
        btn_9 = findViewById(R.id.ch2_calc_btn_9);
        btn_back = findViewById(R.id.ch2_calc_btn_back);
        btn_clear = findViewById(R.id.ch2_calc_btn_clear);
        btn_add = findViewById(R.id.ch2_calc_btn_add);
        btn_sub = findViewById(R.id.ch2_calc_btn_sub);
        btn_mul = findViewById(R.id.ch2_calc_btn_mul);
        btn_div = findViewById(R.id.ch2_calc_btn_div);
        btn_point = findViewById(R.id.ch2_calc_btn_point);
        btn_equal = findViewById(R.id.ch2_calc_btn_equal);
    }
}
